home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’91 / AliasThis! / AliasThisƒ / src / SampleFileIO.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-20  |  8.4 KB  |  313 lines  |  [TEXT/MPS ]

  1.  
  2. #include "SampleDefs.h"            /* C structures we define        */
  3. #include "Sample.h"                /* defines common to C and Rez    */
  4. #include "SampleErrors.h"
  5. #include "SampleMain.h"
  6. #include "SampleFileIO.h"
  7.  
  8. #include <Errors.h>
  9. #include <Memory.h>
  10. #include <Aliases.h>
  11. #include <ToolUtils.h>
  12. #include <Script.h>
  13. #include <Finder.h>
  14. #include <Resources.h>
  15.  
  16.  
  17. #pragma segment Main
  18. /************************************************************************************
  19.  
  20.     ConvertOldToNewSFReply
  21.  
  22.     struct StandardFileReply {            struct SFReply {
  23.         Boolean     sfGood;                <-    Boolean good;
  24.         Boolean     sfReplacing;        <-    Boolean copy;
  25.         OSType         sfType;                <-    OSType fType;
  26.         FSSpec        sfFile;
  27.                         vRefNum;        <-    real vRefnum from (short vRefNum)
  28.                         parID;            <-    real dirID from (short vRefNum)
  29.                         name;            <-    Str63 fName;
  30.         ScriptCode    sfScript;            <-    smSystemScript
  31.         short         sfFlags;            <-    0
  32.         Boolean     sfIsFolder;            <-    false
  33.         Boolean     sfIsVolume;            <-    false
  34.         long        sfReserved1;        <-    0
  35.         short        sfReserved2;        <-    0
  36.     };                                    };
  37.  
  38. *************************************************************************************/
  39. void ConvertOldToNewSFReply(SFReply *oldReply, StandardFileReply *newReply)
  40. {
  41.     OSErr        err;
  42.     long        ignoredProcID;
  43.     
  44.     newReply->sfGood        = oldReply->good;
  45.     newReply->sfReplacing    = oldReply->copy;        /* correct assignment? */
  46.     newReply->sfType        = oldReply->fType;
  47.  
  48.     err = GetWDInfo(oldReply->vRefNum,
  49.                     &newReply->sfFile.vRefNum,
  50.                     &newReply->sfFile.parID,
  51.                     &ignoredProcID);
  52.     BlockMove((Ptr) &oldReply->fName, (Ptr) &newReply->sfFile.name, oldReply->fName[0]+1);
  53.  
  54.     /* punt on the rest */
  55.     newReply->sfScript        = smSystemScript;
  56.     newReply->sfFlags        = 0;
  57.     newReply->sfIsFolder    = false;
  58.     newReply->sfIsVolume    = false;
  59.     newReply->sfReserved1    = 0;
  60.     newReply->sfReserved2    = 0;
  61. }
  62.  
  63.  
  64. #pragma segment Main
  65. /************************************************************************************
  66.  
  67.     WriteWindowData
  68.  
  69.     Writes the data in gWindowData to the file referenced by "refnum". The file is
  70.     closed after the data's been written.
  71.  
  72. *************************************************************************************/
  73. OSErr WriteWindowData(short refNum)
  74. {
  75.     long count = sizeof(WindowData);
  76.     OSErr    err;
  77.  
  78.     if (err = FSWrite(refNum, &count, (Ptr) &gWindowData))
  79.         DisplayError(err, nil, "In WriteWindowData, after FSWrite.");
  80.     if (err = FSClose(refNum))
  81.         DisplayError(err, nil, "In WriteWindowData, after FSClose.");
  82.     return err;
  83. }
  84.  
  85. #pragma segment Main
  86. /************************************************************************************
  87.  
  88.     ReadWindowData
  89.  
  90.     Reads the data from the file referenced by "refnum" into gWindowData. The file is
  91.     closed after the data's been read.
  92.  
  93. *************************************************************************************/
  94. OSErr ReadWindowData(short refNum)
  95. {
  96.     long count = sizeof(WindowData);
  97.     OSErr err;
  98.  
  99.     if (err = FSRead(refNum, &count, (Ptr) &gWindowData))
  100.         DisplayError(err, nil, "In ReadWindowData, after FSRead.");
  101.     if (err = FSClose(refNum))
  102.         DisplayError(err, nil, "In ReadWindowData, after FSClose.");
  103.  
  104.     return err;
  105. }
  106.  
  107. #pragma segment Main
  108. /************************************************************************************
  109.  
  110.     Create_OpenFile
  111.  
  112.     Opens the file specified by the passed FSSpec, creating it if it doesn't
  113.     already exist. Refturns the refnum of the open file to the application.
  114.     File Manager errors are reported and returned.
  115.  
  116. *************************************************************************************/
  117. OSErr Create_OpenFile(FSSpec *file, short *refNum)
  118. {
  119.     OSErr    err;
  120.  
  121.     if ((err = HCreate(file->vRefNum, file->parID, file->name, 'DTS ', 'BOOL')) == dupFNErr) {
  122.  
  123.         /* the user already told Standard File to replace the old file
  124.            so let's get rid of it */
  125.  
  126.         if (err = HDelete(file->vRefNum, file->parID, file->name))
  127.             DisplayError(err, nil, "In Create_OpenFile, after HDelete.");
  128.  
  129.         /* try again */
  130.         else if (err = HCreate(file->vRefNum, file->parID, file->name, 'DTS ', 'BOOL'))
  131.             DisplayError(err, nil, "In Create_OpenFile, after HCreate (again).");
  132.     }
  133.  
  134.     if ( !err )
  135.         err = HOpen(file->vRefNum, file->parID, file->name, fsRdWrPerm, refNum);
  136.         if (err)
  137.             DisplayError(err, nil, "In Create_OpenFile, after HOpen.");
  138.  
  139.     return err;
  140. }
  141.  
  142. #pragma segment Main
  143. /************************************************************************************
  144.  
  145.     DisplayPutFile
  146.  
  147.     Displays the StandardFile PutFile dialog box. Fills out the passed reply
  148.     record, and returns the sfGood field as a result.
  149.  
  150. *************************************************************************************/
  151. Boolean DisplayPutFile(StandardFileReply *reply)
  152. {
  153.     Str255        prompt;
  154.     Str255      origName;
  155.     Point        where = {100, 100};
  156.     SFReply        oldReply;
  157.  
  158.     GetIndString(prompt,kMiscStrings,kSFprompt);
  159.     GetIndString(origName,kMiscStrings,korigName);
  160.  
  161.     if (gHasNewStdFile)
  162.         StandardPutFile(prompt, origName, reply);
  163.     else {
  164.         SFPutFile(where, prompt, origName, nil, &oldReply);
  165.         ConvertOldToNewSFReply(&oldReply, reply);
  166.     }
  167.  
  168.     return reply->sfGood;
  169. }
  170.  
  171. #pragma segment Main
  172. /************************************************************************************
  173.  
  174.     SaveFile
  175.  
  176.     Given a window pointer, this routine saves its state to disk. We first ask
  177.     the user for the name of a file to write to. If the user doesn't press
  178.     Cancel, we create the file (or overwrite an existing one), write the data
  179.     to it, and close the file. We also change the name of the window to be the
  180.     same as the file we just created.
  181.  
  182. *************************************************************************************/
  183. Boolean SaveFile(WindowPtr window)
  184. {
  185.     StandardFileReply    reply;
  186.     short                refNum;
  187.     OSErr                err;
  188.  
  189.     if ( DisplayPutFile(&reply) ) {
  190.         ExtractWindowData(window);
  191.         if (err = Create_OpenFile(&reply.sfFile, &refNum))
  192.             DisplayError(err, nil, "In SaveFile, after Create.");
  193.         else if (err = WriteWindowData(refNum))
  194.             DisplayError(err, nil, "In SaveFile, after Write.");
  195.  
  196.         SetWTitle(window, reply.sfFile.name);
  197.     }
  198.  
  199.     return reply.sfGood;
  200. }
  201.  
  202.  
  203. #pragma segment Main
  204. /************************************************************************************
  205.  
  206.     DisplayGetFile
  207.  
  208.     Simple routine to display a list of files with our file type.
  209.  
  210. *************************************************************************************/
  211. Boolean DisplayGetFile(StandardFileReply *reply)
  212. {
  213.     SFTypeList    typeList = {'BOOL'};
  214.     Point        where = {100, 100};
  215.     SFReply        oldReply;
  216.  
  217.     if (gHasNewStdFile)
  218.         StandardGetFile(nil, 1, typeList, reply);
  219.     else {
  220.         SFGetFile(where, "\pSelect a document to open.", nil, 1, typeList, nil, &oldReply);
  221.         ConvertOldToNewSFReply(&oldReply, reply);
  222.     }
  223.  
  224.     return reply->sfGood;
  225. }
  226.  
  227.  
  228. #pragma segment Main
  229. /************************************************************************************
  230.  
  231.     OpenFile
  232.  
  233.     Given a pointer to an AEDesc, this routine checks to see if it's nil. If so,
  234.     then we bring up a StandardFile dialog box asking for a name. If the pointer
  235.     is NOT nil, we extract from it an FSSpec. In either case, we should end up
  236.     with an FSSpec of a file to open. We open it, read the data from it, and use
  237.     that data to create a new window.
  238.  
  239. *************************************************************************************/
  240. OSErr OpenFile(FSSpecPtr fileToOpen)
  241. {
  242.     OSErr                err;
  243.     StandardFileReply    reply;
  244.     Boolean             GotReply;
  245.     FSSpec                myFileSpec;
  246.     short                fileRefNum;
  247.     
  248.     FSSpec                appleMenuFolder;
  249.     short                vRefNum;
  250.     short                foundVRefNum;
  251.     long                foundDirID;
  252.     AliasHandle            alias;
  253.     short                fRef;
  254.     FInfo                fInfo;
  255.     
  256.  
  257.     if ( !fileToOpen ) {
  258.         if (GotReply = DisplayGetFile(&reply))
  259.             BlockMove((Ptr) &reply.sfFile, (Ptr) &myFileSpec, sizeof(FSSpec));
  260.     }
  261.     else {
  262.         GotReply = true;
  263.         BlockMove((Ptr) fileToOpen, (Ptr) &myFileSpec, sizeof(FSSpec));
  264.     }
  265.  
  266.     if ( GotReply ) {    // we shouldn't be doing this everytime we open a file, but it's late & it's a hack!
  267.         err = FindFolder(-1,kAppleMenuFolderType,false,&foundVRefNum, &foundDirID);
  268.         if (!err)
  269.         {
  270.             err = NewAlias(nil,&myFileSpec,&alias);
  271.         }
  272.         
  273.         if (!err)
  274.         {
  275.             HCreateResFile(foundVRefNum, foundDirID, myFileSpec.name);
  276.             err = ResError();
  277.         }
  278.         
  279.         if (!err)
  280.         {
  281.             fRef = HOpenResFile(foundVRefNum, foundDirID, myFileSpec.name, fsRdWrPerm);
  282.             err = ResError();
  283.         }
  284.         
  285.         if (!err)
  286.         {
  287.             AddResource( (Handle)alias, 'alis', 0,myFileSpec.name);
  288.             err = ResError();
  289.         }
  290.         
  291.         if (!err)
  292.         {
  293.             CloseResFile(fRef);
  294.             err = ResError();
  295.         }
  296.         
  297.         if (!err)
  298.         {
  299.             err = FSpGetFInfo(&myFileSpec, &fInfo);
  300.         }
  301.         
  302.         if (!err)
  303.         {
  304.             fInfo.fdFlags |= 1<<15;
  305.             fInfo.fdFlags &= ~(1<<8);
  306.             err = HSetFInfo(foundVRefNum, foundDirID, myFileSpec.name, &fInfo);
  307.         }
  308.         
  309.         gQuit = true;
  310.     }
  311.     return(err);
  312. }
  313.